package com.zwr.sheller.shop.service.impl;

import com.zwr.sheller.core.common.LSYBusinessException;
import com.zwr.sheller.core.common.LSYExceptionCode;
import com.zwr.sheller.core.util.JsonUtil;
import com.zwr.sheller.shop.service.RemarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author LSY
 * @Date 2022/1/14 14:36
 * @Version 1.0
 **/
@Service
public class RemarkServiceImpl implements RemarkService {

    RedisTemplate<String, Object> template;
    ValueOperations<String, Object> stringOps;
    @Autowired
    public RemarkServiceImpl(RedisTemplate<String, Object> template) {
        this.template = template;
        stringOps = template.opsForValue();
    }

    @Override
    public List<Map<String, Object>> findGoodsRemark(Long sid) throws LSYBusinessException {
        Set<String> keys = template.keys("remark:*" + ":" + "*" + ":" + sid + "*");

        List<Map<String, Object>> remarks = new ArrayList<>();
        if(keys != null) {
            for (String key : keys) {
                Map<String, Object> map = JsonUtil.stringToObject(String.valueOf(stringOps.get(key)), Map.class);

                map.put("rid", key.substring(key.lastIndexOf(":")));
                map.remove("uid");
                map.remove("goodsImg");
                map.remove("skuNum");
                map.remove("skuName");
                map.remove("images");

                remarks.add(map);
            }
        }
        else {
            throw new LSYBusinessException(LSYExceptionCode.NO_REMARK);
        }
        return remarks;
    }

    @Override
    public void auditRemark(String rid) throws LSYBusinessException {
        Set<String> keys = template.keys("*" + rid);

        if(keys != null) {
            for (String key : keys) {
                Map<String, Object> map = JsonUtil.stringToObject(String.valueOf(stringOps.get(key)), Map.class);

                map.put("isAudit", true);

                stringOps.set(key, JsonUtil.objectToString(map));
            }
        }
        else {
            throw new LSYBusinessException(LSYExceptionCode.UNKNOWN_EXCEPTION);
        }
    }

    @Override
    public void replyRemark(String rid, String reply, Boolean isAudit) throws LSYBusinessException {
        Set<String> keys = template.keys("*" + rid);

        if(keys != null) {
            for (String key : keys) {
                Map<String, Object> map = JsonUtil.stringToObject(String.valueOf(stringOps.get(key)), Map.class);

                map.put("isAudit", isAudit);
                map.put("reply", reply);

                stringOps.set(key, JsonUtil.objectToString(map));
            }
        }
        else {
            throw new LSYBusinessException(LSYExceptionCode.UNKNOWN_EXCEPTION);
        }
    }

    @Override
    public void deleteRemark(String rid) throws LSYBusinessException {
        Set<String> keys = template.keys("*" + rid);

        if(keys != null) {
            template.delete(keys);
        }
        else {
            throw new LSYBusinessException(LSYExceptionCode.UNKNOWN_EXCEPTION);
        }
    }
}
